﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using MyCommerce.Presentation.Stores.IViews;
using MyCommerce.Business.GeneralParameters;
using MyCommerce.Business.Stores;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class ProductPriceFillPresenter
    {
        private IProductPriceFill pViewToPresent;

        public ProductPriceFillPresenter(IProductPriceFill vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnLoadView += new GenericEventHandler<ProductPrice>(pViewToPresent_OnLoadView);
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
        }

        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            try
            {
                ProductPriceManager vProductPriceManager = new ProductPriceManager();
                ProductPrice objProductPriceToSave = pViewToPresent.Get();
                if (pViewToPresent.OptionViewSelect == OptionSelect.Create)
                {
                    vProductPriceManager.SaveProductPrice(objProductPriceToSave);
                }
                else
                {
                    objProductPriceToSave.IdProductPrice = pViewToPresent.IdItemEdit;
                    vProductPriceManager.UpdateProductPrice(objProductPriceToSave);
                }
                pViewToPresent.CloseAndSetResult(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<ProductPrice> e)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            pViewToPresent.ColMoney = vCommonClassifierManager.GetClassifierByIDType(1);
            SupplierManager vSupplierManager = new SupplierManager();
            pViewToPresent.ColSupplier = vSupplierManager.GetSuppliers();
            ProductManager vProductManager = new ProductManager();
            pViewToPresent.ColProduct = vProductManager.GetProducts();


            if (e.EventData != null)
                pViewToPresent.Set(e.EventData);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }
    }
}
